import { getPage, getImage } from '../../utils/config';
import { http, promisefy } from '../../utils/utils';
import api from '../../utils/api';
import wxApi from '../../utils/wxApi';
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        loading: 0,
        nameFix: false,
    },

    /**
     * 首页/查看评价--路由跳转
     */
    jumpPageClick(e) {
        const { dataset } = e.currentTarget || e.target;
        if(dataset.id == 1){
            wxApi.switchTab(getPage.home);
        }else if(dataset.id == 2){
            wxApi.redirectTo(getPage.comments);
        }
    },

    /**
     * 评论--路由跳转
     */
    commentsClick(e) {
        const { dataset } = e.currentTarget || e.target;
        const { upperInfo, lowerInfo } = this.data;
        const pageUrl = dataset.id == 0 ? getPage.commentsEdit : getPage.commentsAppend;
        const data = dataset.id == 0 ? upperInfo[dataset.index] : lowerInfo[dataset.index];

        let products = [{
            commentId: data.commentid,
            itemId: data.item_id,
            pic: data.product_img,
            title: data.productname,
        }];

        wxApi.redirectTo(pageUrl, {
            products: JSON.stringify(products),
            shopName: data.shopname,
            orderId: data.orderid,
        });
    },

    /**
     * 页面滚动触发事件--监听页面滚动
     */
    onPageScroll(e) {
        const h = 28;
        const { scrollTop } = e;
        const { nameFix } = this.data;

        if(scrollTop > h && !nameFix){
            wx.setNavigationBarTitle({
                title: '评价成功',
            });

            this.setData({ nameFix: true })
        } else if (scrollTop < h && nameFix){
            wx.setNavigationBarTitle({
                title: '',
            });

            this.setData({ nameFix: false })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        //获取用户信息
        app.getLogin().then((res) => {
            const { user } = res.data;
            const userId = user.user_id;

            http.post(api.default, {
                data: {
                    action: 'getcommentsgoods',
                    userid: userId,
                }
            }, false).then((res) => {
                const { data } = res.data;

                this.setData({
                    userId,
                    upperInfo: data.waitcomments,
                    lowerInfo: data.waitmorecomments,
                    loading: 1,
                })
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    }
})